package com.ymt.bpm.web.rest.platform.fauth;

import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.model.PPos;
import com.ymt.bpm.service.platform.fauth.FaPosService;
import com.ymt.bpm.util.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.QueryParam;
import java.util.Map;

/**
 * Created by Johnny on 2017/2/20.
 */
@RestController
@RequestMapping("/api/v1/fa")
public class FaPosResource {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FaPosService ps;

    /**
     * query all positions
     * @param req
     * @param queryParams
     * @param timezoneOffset
     * @return
     */
    @RequestMapping("/pos/all")
    public JSONObject allByTenant(HttpServletRequest req,
                                  @QueryParam("queryParams")String queryParams,
                                  @QueryParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("allByTenant tenantId=" + tenantId);
            log.debug("allByTenant queryParams=" + queryParams);
            log.debug("allByTenant timezoneOffset=" + timezoneOffset);
        }
        return ps.allByTenant(tenantId, queryParams, timezoneOffset);
    }

    /**
     * get a position
     * @param req
     * @param posId
     * @return
     */
    @RequestMapping(value="/pos/{posId}", method= RequestMethod.GET)
    public JSONObject get(HttpServletRequest req,
                             @PathVariable("posId")String posId,
                          @QueryParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("get tenantId=" + tenantId);
            log.debug("get posId=" + posId);
        }
        return ps.get(tenantId, posId, Const.LANG_ZH, timezoneOffset);
    }

    /**
     * check an attribute of a position
     * @param req
     * @param posId
     * @return
     */
    @PostMapping(value="/pos/checkPosName",
            produces = "plain/text")
    public String checkPosName(HttpServletRequest req,
                          @RequestParam(value="posId", required=false)String posId,
                          @RequestParam("posName")String posName) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("checkPosName tenantId=" + tenantId);
            log.debug("checkPosName posName=" + posName);
            log.debug("checkPosName posId=" + posId);
        }
        return ps.checkAttr(tenantId, "PosName", posName, posId);
    }

    /**
     * save a position
     * @param req
     * @param pos
     * @return
     */
    @RequestMapping(value="/pos/save", method= RequestMethod.POST)
    public JSONObject saveDetailed(HttpServletRequest req,
                                   PPos pos) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("save tenantId=" + tenantId);
            log.debug("save pos.getPosId()=" + pos.getPosId());
            log.debug("save pos.getPosName()=" + pos.getPosName());
            log.debug("save pos.getIsInheritable()=" + pos.getIsInheritable());
        }
        return ps.save(tenantId, loginName, displayName, pos);
    }

    /**
     * delete a position
     * @param req
     * @param posId
     * @return
     */
    @RequestMapping(value="/pos/delete/{posId}", method= RequestMethod.DELETE)
    public JSONObject delete(HttpServletRequest req,
                             @PathVariable("posId")String posId) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("delete tenantId=" + tenantId);
            log.debug("delete posId=" + posId);
        }
        return ps.delete(tenantId, loginName, displayName, posId);
    }

    @RequestMapping(value = "/pos/queryDhx", method = RequestMethod.POST)
    public String queryDhx(HttpServletRequest req,
                        @RequestParam("queryParams")String queryParams,
                        @RequestParam("fromRow")int fromRow,
                        @RequestParam("toRow")int toRow,
                        @RequestParam("timezoneOffset")int timezoneOffset,
                        @RequestParam("ordercolumns")String ordercolumns) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String lang = userDetail.get(Const.LANG);
        if (log.isDebugEnabled()) {
            log.debug("queryDhx tenantId=" + tenantId);
            log.debug("queryDhx lang=" + lang);
            log.debug("queryDhx queryParams=" + queryParams);
        }
        return ps.queryDhx(tenantId, queryParams, fromRow, toRow, timezoneOffset, lang, ordercolumns);
    }

}
